package cn.edu.sgu.www.pms.service.impl;

import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.dto.MenuDTO;
import cn.edu.sgu.www.pms.entity.Menu;
import cn.edu.sgu.www.pms.mapper.MenuMapper;
import cn.edu.sgu.www.pms.pager.Pager;
import cn.edu.sgu.www.pms.pager.impl.MenuPager;
import cn.edu.sgu.www.pms.service.MenuService;
import cn.edu.sgu.www.pms.util.ObjectUtils;
import cn.edu.sgu.www.pms.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class MenuServiceImpl implements MenuService {

    private final MenuMapper menuMapper;

    @Autowired
    public MenuServiceImpl(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public void insert(MenuDTO menuDTO) {
        Menu menu = toEntity(menuDTO);

        menu.setId(StringUtils.uuid());

        menuMapper.insert(menu);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        menuMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(MenuDTO menuDTO) {
        ObjectUtils.requireNonNull(menuDTO.getId(), CustomMessages.ID_NOT_NULL);

        Menu menu = toEntity(menuDTO);

        menuMapper.updateById(menu);
    }

    @Override
    public List<Menu> selectParent() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();

        wrapper.eq("type", 0);
        wrapper.orderByAsc("service_id");

        return menuMapper.selectList(wrapper);
    }

    @Override
    public Menu selectById(String id) {
        return menuMapper.selectById(id);
    }

    @Override
    public Page<Menu> selectByPage(MenuPager pager) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        Page<Menu> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getServiceId() != null,
                "service_id", pager.getServiceId()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getParentId()),
                "parent_id", pager.getParentId()
        );
        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.like(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );

        return menuMapper.selectPage(page, queryWrapper);
    }

    private Menu toEntity(MenuDTO menuDTO) {
        Menu menu = new Menu();

        menu.setId(menuDTO.getId());
        menu.setUrl(menuDTO.getUrl());
        menu.setName(menuDTO.getName());
        menu.setIconClass(menuDTO.getIconClass());
        menu.setType(menuDTO.getType());
        menu.setParentId(menuDTO.getParentId());
        menu.setServiceId(menuDTO.getServiceId());

        return menu;
    }

}